﻿using System;
using System.Collections.Generic;
using System.Linq;
using Dqaqs.Common;

namespace Dqaqs.Data.Extensions
{
	public static class SchemaExtensions
	{
		public static ILookup<string, string> GetMetrics(this Schema schema, SchemaTable table)
		{
			ArgumentCheck.NotNull(schema, "schema");
			ArgumentCheck.NotNull(table, "table");
			var tbl = schema.Tables.FirstOrDefault(t => t == table);
			return tbl.Columns.Where(c => c.Name.Contains("."))
													.Select(c => new { col = c.Name.Split(".".ToCharArray())[0], metric = c.Name.Split('.')[1] })
													.ToLookup(c => c.col, c => c.metric);
		}
		
		public static IEnumerable<string> GetMetricsFor(this Schema schema, SchemaColumn col)
		{
			ArgumentCheck.NotNull(col, "column");
			ArgumentCheck.NotNull(schema, "schema");
			var tbl = schema.Tables.FirstOrDefault(t => t.Columns.Contains(col));
			if (tbl == null)
				throw new InvalidOperationException(string.Format("Column {0} not found in the schema.", col.Name));
			return tbl.Columns.Where(c => c.Name.Contains("."))
				.Select(c => c.Name.Split(".".ToCharArray())[1]);
		}
	}

}
